package edu.xhu.communityantiepidemicsystemback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xhu.communityantiepidemicsystemback.dto.UpdatePwd;
import edu.xhu.communityantiepidemicsystemback.entity.Staff;
import edu.xhu.communityantiepidemicsystemback.mapper.StaffMapper;
import edu.xhu.communityantiepidemicsystemback.service.AuthService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;

@Service
public class AuthServiceImpl extends ServiceImpl<StaffMapper, Staff> implements AuthService {

    @Override
    public boolean checkLoginAccount(Staff staff) {
        QueryWrapper<Staff> wrapper=new QueryWrapper<>();
        wrapper.eq("account",staff.getAccount()).eq("password",staff.getPassword());
        int count = this.count(wrapper);
        if(count==1){
            return true;
        }
        return false;
    }

    @Override
    public Staff getInfoByAccount(String account) {
        QueryWrapper<Staff> wrapper=new QueryWrapper<>();
        wrapper.eq("account",account);
        Staff staff = this.getOne(wrapper);
        return staff;
    }

    @Override
    public boolean updatePwdByAccount(UpdatePwd updatePwd) {
        Staff staff=new Staff();
        staff.setAccount(updatePwd.getAccount());
        staff.setPassword(updatePwd.getNewpassword1());
        staff.setUpdateTime(LocalDateTime.now());
        QueryWrapper<Staff> wrapper=new QueryWrapper<>();
        wrapper.eq("account",staff.getAccount());
        return this.update(staff,wrapper);
    }
}
